<template>
  <div class="specs-list">
    <!-- data：表格中的数据 -->
    <!-- default-expand-all：默认展开所有节点 -->
    <!-- tree-props：树形结构的配置项 -->
    <el-table
      v-if="isOk && tableData.length"
      :data="tableData"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="id" label="ID" width="60"> </el-table-column>
      <el-table-column prop="specsname" label="属性名称" width="100"> </el-table-column>
      <el-table-column prop="attr" label="属性值" width="200">
        <template slot-scope="scope">
          <el-tag class="tag" v-for="(item,index) in scope.row.attrs" :key="index">
            {{item}}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.status === 1">正常</el-tag>
          <el-tag type="danger" v-else>禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="small" circle @click="edit(scope.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" size="small" circle @click="del(scope.row.id)"></el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { getSpecsList } from "../../api";
export default {
  data() {
    return {
      isOk: false,
      // 表格中的数据
      tableData: [],
    };
  },
  created() {
    // 获取菜单列表
    this.getTabelList();
  },
  methods: {
    async getTabelList() {
      let res = await getSpecsList();
      // console.log(res);
      this.tableData = res.list || [];
      // 数据 已经获取到了
      this.isOk = true;
    },
    del(id) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        // 点击确认按钮
        .then(async () => {
          // 真的删除
          let res = await delMenu(id);
          if (res.code === 200) {
            // code是200表示成功
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          }else {{
            this.$message({
              type: "warning",
              message: res.msg,
            });
          }}
          // 更新页面内容
          if(res.list.length) {
            this.tableData = res.list;
          }else {
            this.getTabelList();
          }
        })
        // 点击取消按钮
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
      console.log(id);
    },
    edit(data) {
      // 子传父
      this.$emit('edit', data)
    }
  },
};
</script>

<style>
.specs-list .tag {
  margin-left: 10px;
}
</style>